#include "queue.h"

void Queue::enQueue(int value){
	try {
		NodePtr newNode = new QueueNode(value);
		if (isEmpty()) {
			front = rear = newNode;
		} else {
			rear->next = newNode;
			rear = newNode;
		}
		++size;
	} catch (const std::bad_alloc& e) {
		std::cerr << "内存分配失败: " << e.what() << std::endl;
	}
}

void Queue::deQueue(){
	if(isEmpty()){
		std::cout<<"队列为空，无法进行出队操作"<<std::endl;
		return;
	}
	NodePtr temp = front;
	front = front->next;
	//front达到队尾
	if(front == nullptr)
		rear = nullptr;
	delete temp;
	--size;
}

int Queue::frontValue(){
	if(isEmpty()){
		std::cout<<"队列为空"<<std::endl;
		return -1;
	}
	else
		return front->val;
}

int Queue::getBackValue() const{
	if(isEmpty()){
		std::cout<<"队列为空"<<std::endl;
		return -1;
	}
	return rear->val;
}

void Queue::clear(){
	while(!isEmpty()){
		deQueue();
	}
}



